//
//  TRSystemSettingViewController.m
//  WeiMiLan
//
//  Created by anan on 14/11/23.
//  Copyright (c) 2014年 Mac. All rights reserved.
//

#import "TRSystemSettingViewController.h"

@interface TRSystemSettingViewController ()

@end

@implementation TRSystemSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self initNavigation];
    
    self.labelVersion.text = [QLPublic getVersionString];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    
    NSDictionary *userInfo = [[QLUserManager shareInstance] getLastUserInfoFromNative];
    if (userInfo) {
        NSString *headerUrl = [userInfo ql_stringForKey:@"HEAD_PIC_URL"];
        if (headerUrl==nil||[headerUrl rangeOfString:@"/null"].location!=NSNotFound) {
            UIAlertView *alert = [[UIAlertView alloc] init];
            [alert setTitle:@"提示"];
            [alert setMessage:@"请更新昵称和头像"];
            [alert addButtonWithTitle:@"确定"];
            [alert addButtonWithTitle:@"忽略"];
            alert.tag=1;
            alert.delegate = self;
            [alert show];
        }
        
        NSString *phoneNum = [userInfo ql_stringForKey:@"PHONE"];
        if (phoneNum==nil||phoneNum.length<11) {
            UIAlertView *alert = [[UIAlertView alloc] init];
            [alert setTitle:@"提示"];
            [alert setMessage:@"请绑定手机，以免丢失账号"];
            [alert addButtonWithTitle:@"确定"];
            [alert addButtonWithTitle:@"忽略"];
            alert.tag=2;
            alert.delegate = self;
            [alert show];
        }
    }
}

#pragma mark --UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==1) {
        if (buttonIndex == 0) {
            TRUserDetailViewController *vc = [[TRUserDetailViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    if (alertView.tag==2) {
        if (buttonIndex == 0) {
            TRUserDetailViewController *vc = [[TRUserDetailViewController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initNavigation
{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    self.weChatNavigationBar=[[WeChatNavigationBar alloc] init];
    [self.view addSubview:self.weChatNavigationBar];
    self.weChatNavigationBar.titleLabel.text=@"设置";
    [self.weChatNavigationBar.leftButton addTarget:self action:@selector(exit) forControlEvents:UIControlEventTouchUpInside];
    self.weChatNavigationBar.rightButton.hidden = YES;
}

- (void)exit
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)buttonZaixianbangzuAction:(id)sender
{
    [QLPlayerManager openBrowserWithUrl:@"http://www.vsmu.cn/help.html" title:@"在线帮助" userId:nil viewController:self];
}

- (IBAction)buttonGuanyuwomenAction:(id)sender
{
//    [QLPlayerManager openBrowserWithUrl:@"http://www.vsmu.cn/gy.html" title:@"关于我们" viewController:self];
    TRAboutViewController *vc = [[TRAboutViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)buttonXiaoxituisongAction:(id)sender
{
    
}

- (IBAction)buttonHeimingdanAction:(id)sender
{
    
}

- (IBAction)buttonYinshizhengceAction:(id)sender
{
    [QLPlayerManager openBrowserWithUrl:@"http://www.vsmu.cn/privacy.html" title:@"用户隐私条例" userId:nil viewController:self];
}

- (IBAction)buttonShequguizeAction:(id)sender
{
    TRBbsRoleViewController *vc = [[TRBbsRoleViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)buttonYijianfankuiAction:(id)sender
{
    self.feedbackViewController = [UMFeedback feedbackViewController];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.feedbackViewController];
    [[UIApplication
    sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    nav.navigationBar.barStyle = UIBarStyleBlack;
    // 设置左边的按钮
    UIBarButtonItem *returnButton = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(returnWeimilan)];
    [self presentViewController:nav animated:YES completion:nil];
    
    self.feedbackViewController.navigationItem.leftBarButtonItem = returnButton;
}

- (void)returnWeimilan
{
    [[UIApplication
      sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    [self.feedbackViewController dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)buttonQinglihuancunAction:(id)sender
{
    [ApplicationDelegate.rootVC clearCacheAction];
    [SVProgressHUD showErrorWithStatus:@"清除缓存成功"];
}

- (IBAction)buttonDangqianbanbenAction:(id)sender
{
    [QLMaskUtil showMaskLoadingWithView:self.viewContent];
    [MobClick checkUpdateWithDelegate:self selector:@selector(updateCallback:)];
}

- (void)updateCallback:(NSDictionary*)dic
{
    [QLMaskUtil hideLoadingMaskWithView:self.viewContent];
}

- (IBAction)buttonDengchuAction:(id)sender
{
//    TRLoginViewController *lVC=[[QLPlayerManager getStoryboard] instantiateViewControllerWithIdentifier:@"TRLoginViewController"];
    
    NSDictionary *userInfo = [[QLUserManager shareInstance] getLastUserInfoFromNative];
    NSString *account = [QLPropertyUtils getStringByKey:@"account" defaultValue:nil];
    NSString *key = [NSString stringWithFormat:@"userHeader_%@",account];
    [QLPropertyUtils putStringByKey:key value:userInfo[@"HEAD_PIC_URL"]];
    
    [[QLUserManager shareInstance] userLogout];
    TRUserLoginViewController *vc = [[TRUserLoginViewController alloc] init];
    [self presentViewController:vc animated:YES completion:^{
        
    }];
}

@end
